package com.gaoyi.websocket.controller;

import com.gaoyi.websocket.common.ServerResponse;
import com.gaoyi.websocket.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

import static com.gaoyi.websocket.service.impl.MessageServiceImpl.LATEST_MSG;

@RestController
@RequestMapping("/chat")
@Slf4j
@EnableAsync
public class ChatController {

    public static final String SERVER = "server";
    public static final String ASSIST = "assist";

    @Resource
    MessageService messageService;
    @Resource
    RedisTemplate redisTemplate;

    /**
     * 查询跟我发过消息的人
     * */
    @GetMapping("/queryUsed")
    public ServerResponse usedComment(@RequestParam(defaultValue = "1") int pageNo,
                                      @RequestParam(defaultValue = "10")int pageSize,
                                      @RequestParam(value = "id",required = false) String id){

        Map map = messageService.queryUsedMsg(pageNo,pageSize,id);
        return ServerResponse.successResponse(map);
    }

    /**
     * 删除跟我发过消息的人中的某一个
     * */
    @GetMapping("/deleteUsed")
    public ServerResponse deleteUsed(@RequestParam(value = "id",required = true) String id){

        long i = redisTemplate.opsForHash().delete(LATEST_MSG,id);
        return i > 0 ? ServerResponse.success() : ServerResponse.errorMessage("删除失败!");
    }

    /**
     * 遍历在线用户
     * */
    @GetMapping("/scanOnline")
    public ServerResponse scanOnlineUsers(@RequestParam(defaultValue = "1") int pageNo,
                                          @RequestParam(defaultValue = "100")int pageSize){

        return ServerResponse.successResponse(messageService.scanOnlineUsers(pageNo,pageSize));
    }
}




















